import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 国际化相关代码
try {
	// 1. 分析用户已经选择的语言 
	var userLang = uni.getStorageSync("userLang");
	// 2. 如果用户没有选择过获取用户手机的语言
	if(!userLang){
		const sys = uni.getSystemInfoSync();
		userLang = sys.language;
	}
	console.log(userLang);
	// 以中英文切换为例, 其他语言请使用 getSystemInfoSync 获取语言对应的字符串
	// 然后扩展语言包即可
	if(userLang.substring(0,2) == 'zh'){
		var lang = require('../lang/Chinese.js');
	}else{
		var lang = require('../lang/English.js');
	}
} catch (e) {
    // error
}
const store = new Vuex.Store({
	state: {
		themeColors:"#005883",   //   主题颜色
		isPermissions:"0", //   是否需要蓝牙权限
		lang : lang,
		canAdd:true,
		deviceId: null,
		serviceId: null,
		characteristicId: null,
		paramList:[],
		tabIndex: null,
		tabTitle: '',
		sortList: null,
		changeList: null,
		baud:500,
		dataShow: false,
		stateShow: false,
		globalState: false,
		connectState: false
	},
	mutations: {
		changeLang(){
			//  选择菜单部分
			uni.showActionSheet({
				itemList:['简体中文', 'English'],
				success(e){
					if(e.tapIndex == 0){
						lang = require('../lang/Chinese.js');
						uni.setStorageSync('userLang','zh-cn');
					}else{
						lang = require('../lang/English.js');
						uni.setStorageSync('userLang','es')
					}
					// #ifdef APP-PLUS   
					plus.runtime.restart();  //   重启应用  
					// #endif
				}
			})
		},
		setDeviceId(state, item) {
			state.deviceId = item
		},
		setServiceId(state, item) {
			state.serviceId = item
		},
		setCharacteristicId(state, item) {
			state.characteristicId = item
		},
		setParamList(state, item) {
			state.paramList = item
		},
		setTabIndex(state, item) {
			state.tabIndex = item
		},
		setSortList(state, item) {
			state.sortList = item
		},
		setChangeList(state, item) {
			state.changeList = item
		},
		setBaud(state, item) {
			state.baud = item
		},
		setDataShow(state, item) {
			state.dataShow = item
		},
		setStateShow(state, item) {
			state.stateShow = item
		},
		setGlobalState(state, item) {
			state.globalState = item
		},
		setTabTitle(state, item) {
			state.tabTitle = item
		},
		setConnectState(state, item) {
			state.connectState = item
		},
		setMessageAdd(state,item){
			state.canAdd=item;
		},
		setPermissions(state,item){
			state.isPermissions=item;
		},
		setThemeColors(state,item){
			state.themeColors=item;
		},
		reset(state,item){
			state.deviceId=null;
			state.serviceId=null;
			state.characteristicId=null;
			state.paramList=[];
			state.sortList=null;
			state.changeList=null;
			state.dataShow=false;
			state.stateShow=false;
			state.globalState=false;
			state.connectState=false;
		}
	}
})

export default store
